__objs = {}														--所有GUI组件的字典使用C指针作为索引

Justification = {left = 1, right = 2, top = 8, bottom = 16, bottomLeft = 17, bottomRight = 18, centred = 36 }

_msg_handler = function(_ptr, msg, ...)							--通用的消息处理函数
	local component = __objs[_ptr]
	if component[msg] ~= nil then 
		return component[msg](component, ...)
	end 
end

function _enclosure(ptr, base)
	local obj = {}
	obj._ptr = ptr
	setmetatable(obj, {__index = base})
	return obj	
end

function _add_obj(parent, obj)
	obj._parent = parent
	__objs[obj._ptr] = obj
	return obj
end

__graphics = {
	Size = function(self)
		return juce.get_size(self._ptr)
	end,
	DrawText = function(self, x, y, w, h, text, justfication)
		juce.draw_text(self._ptr, x, y, w, h, text, justfication)
	end,
	DrawImage = function(self, name, x, y, w, h)
		juce.draw_image(self._ptr, name, x, y, w, h)
	end,
	SetColor = function(self, color)
		juce.set_color(self._ptr, color)
	end,
	FillRect = function(self, x, y, w, h)
		juce.fill_round_rect(self._ptr, x, y, w, h, 0)
	end,
	FillRoundRect = function(self, x, y, w, h, r)
		juce.fill_round_rect(self._ptr, x, y, w, h, r)
	end,
	FillEllipse = function(self, x, y, w, h)
		juce.fill_ellipse(self._ptr, x, y, w, h)
	end
}

__widget = {
	Size = function(self)
		return juce.widget_size(self._ptr)
	end,
	Show = function(self, show)
		juce.widget_show(self._ptr, show)
	end,
	Repaint = function(self)
		juce.widget_repaint(self._ptr)
	end
}

_app_widget = nil				--整个App的顶层widget 一直存在并且可见

function GetApp()
	if _app_widget == nil then
		_app_widget = _enclosure(juce.widget_new(nil, 0, 0, 0, 0), __widget)
		__objs[_app_widget._ptr] = _app_widget
	end
	return _app_widget
end

function Widget(parent, x, y, w, h)
	local widget = _enclosure(juce.widget_new(parent._ptr, x, y, w, h), __widget)
	return _add_obj(parent, widget)
end

__button = {
	Enable = function(self, enabled)
		juce.button_enable(self._ptr, enabled)
	end,
	SetText = function(self, text)
		juce.button_text(self._ptr, text)
	end
}

function Button(parent, x,y,w,h,text)
	local btn = _enclosure(juce.button_new(parent._ptr, x,y,w,h,text), __button)
	return _add_obj(parent, btn)
end

__editor = {
	GetText = function(self)
		return juce.editor_get_text(self._ptr)
	end
}

function Editor(parent, x,y,w,h)
	local editor = _enclosure(juce.editor_new(parent._ptr, x,y,w,h), __editor)
	return _add_obj(parent, editor)
end

__listview = {
	AddItem = function(self, text, height, color)
		juce.listview_add(self._ptr, text, height, color)
	end,
	Update = function(self)
		juce.listview_update(self._ptr)
	end	
}

function ListView(parent, x,y,w,h)
	local listview = _enclosure(juce.listview_new(parent._ptr, x,y,w,h), __listview)
	return _add_obj(parent, listview)
end

function Tabbed(parent, x, y, w, h)
	local tabbed = Widget(parent, x, y, w, h)
	tabbed:Show(true)
	tabbed.active = 1
	tabbed.Click = function(self, x , y)
		if #self.texts > 0 then
			local w, h = self:Size()
			local tab_size = w / #self.texts
			if y < h and x > 0 and x < w then 
				clicked = math.ceil(x / tab_size)
				if clicked ~= self.active then
					if self["Change"] ~= nil then 
						self["Change"](self, self.active, clicked)
					end
					self.active = clicked
					self:Repaint()
				end
			end 
		end
	end 
	tabbed.Paint = function(self, g)
		if #self.texts > 0 then 
			local w,h = self:Size()
			local tab_size = w / #self.texts
			local pos = 0
			for k, v in pairs(self.texts) do
				if k == self.active then
					g:SetColor(0xFF404000)
				else
					g:SetColor(0xFFA0A000)
				end
				g:FillRect(pos, 0, tab_size, h)
				g:SetColor(0xFFFFFFFF)
				g:DrawText(pos, 0, tab_size, h, v, Justification.centred)
				pos = pos + tab_size
			end
		end
	end
	return tabbed
end
